/**
 * Created by CK on 2017/3/6.
 */
(function () {
    var browser = UM.browser;
    var formulaArr = {
        "formula1":  {"tag": "{/}frac{ }{ }", "title": ""},
        "formula2":  {"tag": "^{ }/_{ }", "title": ""},
        "formula3":  {"tag": "x^{ }", "title": ""},
        "formula4":  {"tag": "x_{ }", "title": ""},
        "formula5":  {"tag": "x^{ }_{ }", "title": ""},
        "formula6":  {"tag": "{/}bar{ }", "title": ""},
        "formula7":  {"tag": "{/}sqrt{ }", "title": ""},
        "formula8":  {"tag": "{/}nthroot{ }{ }", "title": ""},
        "formula9":  {"tag": "{/}sum^{ }_{n=}", "title": ""},
        "formula10": {"tag": "{/}sum", "title": "西格玛"},
        "formula11": {"tag": "{/}log_{ }", "title": ""},
        "formula12": {"tag": "{/}ln", "title": ""},
        "formula13": {"tag": "{/}int_{ }^{ }", "title": "不定积分"},
        "formula14": {"tag": "{/}oint_{ }^{ }", "title": ""}
    };
    $.each(formulaArr, function(key, value){
        UM.plugins[key] = function () {
            var me = this;

            function getActiveIframe() {
                return me.$body.find('iframe.edui-formula-active')[0] || null;
            }

            me.commands[key] = {
                execCommand: function (cmd, latex) {
                    var latex =  value.tag;
                    latex = latex.replace("{/}", "\\");
                    var iframe = getActiveIframe();
                    if (iframe) {
                        iframe.contentWindow.formula.insertLatex(latex);
                    } else {
                        me.execCommand('inserthtml', '<span class="mathquill-embedded-latex">' + latex + '</span>');
                        browser.ie && browser.ie9below && setTimeout(function(){
                            var rng = me.selection.getRange(),
                                startContainer = rng.startContainer;
                            if(startContainer.nodeType == 1 && !startContainer.childNodes[rng.startOffset]){
                                rng.insertNode(me.document.createTextNode(' '));
                                rng.setCursor()
                            }
                        },100)
                    }
                },
                queryCommandState: function (cmd) {
                    return 0;
                },
                queryCommandValue: function (cmd) {
                    var iframe = getActiveIframe();
                    return iframe && iframe.contentWindow.formula.getLatex();
                }
            }

        };
        UM.registerUI(key,
            function(name) {
                var me = this;
                var $btn = $.eduibutton({
                    icon : name,
                    click : function(){
                        me.execCommand(name);
                    },
                    cssRules :'border:1px solid red;',
                    title: value.title
                });

                this.addListener('selectionchange',function(){
                    var state = this.queryCommandState(name);
                    $btn.edui().disabled(state == -1).active(state == 1)
                });
                return $btn;
            }
        );
    });
})();